//
//  AppDelegate.m
//  BaxTer
//
//  Created by 陈武琦 on 16/8/30.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "AppDelegate.h"
#import "BTTabBarVC.h"
#import "BTLoginVC.h"
#import "BTNavigationContrller.h"
#import "CWQ_historySearchVC.h"
#import "AppDelegate+pushHandle.h"
#import "WCBaiduPushBindHelp.h"
#import "WSJShowNewfeatureTool.h"
#import "WSJCrashTool.h"

#if DEBUG
#import "JxbDebugTool.h"
#endif
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
#if DEBUG
   // [[JxbDebugTool shareInstance] setMainColor:[UIColor redColor]];
    //[[JxbDebugTool shareInstance] enableDebugMode];
#endif
    //监视网络请求
//#if defined(DEBUG)||defined(_DEBUG)
//  
//    [[FLEXManager sharedManager] showExplorer];
//
//#endif
    
    
    
    //初始化百度云推送
    [self BPushApplicationWith:application didFinishLaunchingWithOptions:launchOptions];
    
    /*
     
     3D touch选项
     #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_9_0
     [self creatShortcutItem];
     #endif
     */
    
    
    /*
    BTTabBarVC *tabVC = [[BTTabBarVC alloc] init];
    self.window.rootViewController = tabVC;
    [self.window makeKeyAndVisible];
     */
   // /*
    
//    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"login"]) {
//        BTLoginVC *loginVC = [BTLoginVC new];
//        nav = [[BTNavigationContrller alloc] initWithRootViewController:loginVC];
//    }else {
    

//    }
    
    [WSJShowNewfeatureTool handleConfigWithWindow:self.window];
    
    [[WSJCrashTool shareCrashTool] config];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    [self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path]];
    
   // */
    // Override point for customization after application launch.
    return YES;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_9_0
//3D touch选项
- (void)creatShortcutItem {
    //创建系统风格的icon
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    //创建快捷选项
    UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.aqi.search" localizedTitle:@"搜索" localizedSubtitle:@"" icon:icon userInfo:nil];
    
    //添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[item];
}

//处理点击3D touch 方法的功能实现

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    
    
    //
    
    
    if ([shortcutItem.type isEqualToString:@"com.aqi.search"]) {
        
        BTTabBarVC *rootTabVC = (BTTabBarVC *)self.window.rootViewController;
        rootTabVC.selectedIndex = 0;
        BTNavigationContrller *nav = [rootTabVC viewControllers][0];
        NSArray *vcs = [nav viewControllers];
        BOOL is_containSearchVC = NO;
        for (UIViewController *vc in vcs) {
            if ([vc isKindOfClass:[CWQ_historySearchVC class]]) {
                [nav popToViewController:vc animated:NO];
                is_containSearchVC = YES;
            }
        }
        
        if (!is_containSearchVC) {
            CWQ_historySearchVC *historySearchVC = [CWQ_historySearchVC new];
            [nav pushViewController:historySearchVC animated:YES];
        }
        
    }if ([shortcutItem.type isEqualToString:@"com.shengshi.BaxTer.Scan"]) {
        
        //          WSJLog(@"朋友圈");tabbarVC
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UITabBarController *tabbarVC = [story instantiateViewControllerWithIdentifier:@"tabbarVC"];
        [tabbarVC setSelectedIndex:1];
        [self.window.rootViewController presentViewController:tabbarVC animated:YES completion:nil];
        
}}


#endif

//不让iCoud备份数据，跳过ICloud的备份
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        WSJLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}



#pragma mark - Orientation  单个页面横屏

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    
    
    if (self.is_LaunchScreen)
    {
        WSJLog(@"可以旋转了");
        return UIInterfaceOrientationMaskAll;
    }
    else return UIInterfaceOrientationMaskPortrait;
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
